# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=unicorn
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-unicorn"
         "${MINGW_PACKAGE_PREFIX}-python-unicorn")
pkgver=2.1.3
pkgrel=1
pkgdesc="A lightweight multi-platform, multi-architecture CPU emulator framework based on QEMU (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.unicorn-engine.org'
msys2_repository_url="https://github.com/unicorn-engine/unicorn"
msys2_references=(
  "cpe: cpe:/a:unicorn-engine:unicorn_engine"
  "purl: pkg:pypi/unicorn"
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools-scm")
source=("https://github.com/unicorn-engine/unicorn/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-bindings-python.patch"
        "003-aarch64.patch")
sha256sums=('5572eecd903fff0e66694310ca438531243b18782ce331a4262eeb6f6ad675bc'
            '0fbc9659d86bf9d064aa0eb2f69d2f2afaed83d96ebbdff656f65255522f476d'
            'ff1f1fc64c8c2d8c6f4633078290c16c84b78ad6b9521ebfd35ee18e64ceefe9')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    001-bindings-python.patch \
    003-aarch64.patch
}

build() {
  # Build unicorn
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  # Build bindings
  cp -r "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/python-build-${MSYSTEM}
  cd "${srcdir}"/python-build-${MSYSTEM}
  cd bindings
  python const_generator.py python

  cd python
  export SETUPTOOLS_SCM_PRETEND_VERSION="${pkgver}"
  LIBUNICORN_PATH="/doesnotexist" \
    python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package_unicorn() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LGPL2" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LGPL2"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING_GLIB" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING_GLIB"
}

package_python-unicorn() {
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-unicorn")

  cd "python-build-${MSYSTEM}"/bindings/python

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
